//
//  ReweetedStatusView.m
//  weibo-OC
//
//  Created by diyuyma on 16/3/20.
//  Copyright © 2016年 zhangkaiyun. All rights reserved.
//

#import "ReweetedStatusView.h"
#import "StatusPictureView.h"
#import "Masonry.h"
#import "Status.h"

@interface ReweetedStatusView ()

@property (nonatomic, strong)UILabel *retweetedLabel;
@property (nonatomic, strong)StatusPictureView *pictureView;
@property (nonatomic, strong)MASConstraint *bottomConstraints;

@end

@implementation ReweetedStatusView

- (void)setRetweetedStatus:(Status *)retweetedStatus {
    self.retweetedLabel.text = retweetedStatus.text;
    [self.bottomConstraints uninstall];
    
    //给配图视图设置数据源
    NSArray *picurls = retweetedStatus.imageURLs;
    if (picurls.count > 0) {
        // 有配图视图
        self.pictureView.imageURLs = retweetedStatus.imageURLs;
        //显示配图视图
        self.pictureView.hidden = false;
        // 更新约束
        [self mas_updateConstraints:^(MASConstraintMaker *make) {
            self.bottomConstraints = make.bottom.equalTo(self.pictureView.mas_bottom).offset(StatusCellMargin);
        }];
    } else {
        // 没有配图视图
        self.pictureView.hidden = true;
        [self mas_updateConstraints:^(MASConstraintMaker *make) {
            self.bottomConstraints = make.bottom.equalTo(self.retweetedLabel.mas_bottom).offset(StatusCellMargin);
        }];
    }
}

// MARK: 3.重写父类的构造方法 调用设置 UI
- (instancetype)initWithFrame:(CGRect)frame {
    self = [super initWithFrame:frame];
    self.backgroundColor = [UIColor colorWithWhite:0.95 alpha:1];
    [self setupUI];
    return self;
}

// MARK: 2.设置 UI
- (void)setupUI {
    // 添加子控件
    [self addSubview:self.retweetedLabel];
    [self addSubview:self.pictureView];
    // 设置约束
    [self.retweetedLabel mas_makeConstraints:^(MASConstraintMaker *make) {
        make.top.equalTo(self.mas_top).with.offset(StatusCellMargin);
        make.left.equalTo(self.mas_left).with.offset(StatusCellMargin);
    }];
    
    [self.pictureView mas_makeConstraints:^(MASConstraintMaker *make) {
        make.top.equalTo(self.retweetedLabel.mas_bottom).with.offset(StatusCellMargin);
        make.left.equalTo(self.retweetedLabel.mas_left);
    }];
    
    [self mas_makeConstraints:^(MASConstraintMaker *make) {
        self.bottomConstraints = make.bottom.equalTo(self.pictureView.mas_bottom).with.offset(StatusCellMargin);
    }];
}

// MARK: 1.懒加载所有子控件
- (UILabel *)retweetedLabel {
    if (_retweetedLabel == nil) {
        _retweetedLabel = [[UILabel alloc] init];
        [_retweetedLabel initWithLabel:@"我是转发微博" WithColor:[UIColor darkGrayColor] WithSize:14 WithMargin:StatusCellMargin];
    }
    return _retweetedLabel;
}

- (StatusPictureView *)pictureView {
    if (_pictureView == nil) {
        _pictureView = [[StatusPictureView alloc] init];
    }
    return _pictureView;
}
@end
